package press.bolin.game;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * 玩家类 继承游戏对象
 */
public class Champion extends GameObject {

    // 移动 4方向
    private boolean up, down, left, right;
    // 移动动画
    static String[] imgs = new String[6];
    int moveCount = 0;

    static {
        // 初始化图片
        for (int i = 0; i < imgs.length; i++) {
            imgs[i] = "img/Play/R-C_" + (i + 1) + ".jpg";
        }
    }


    public Champion(GameFrame gameFrame) {
        super(gameFrame);
        // 设置图片
        setImg("img/Play/R-C_1.jpg");
        // 设置坐标
        setX(300);
        setY(300);
        // 设置移动速度
        setSpeed(20);
    }

    /**
     * 碰撞范围
     * @return
     */
    @Override
    public Rectangle getRec() {
        return new Rectangle(getX(), getY(), 0, 0);
    }


    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(getImg(), getX(), getY(), null);
        g.setColor(Color.GREEN);
        g.fillOval(getX() + 32, getY() + 65, 10, 10);
        g.drawRect(getX(), getY(), 72, 138);
        move();
    }

    public void move() {
        if (up) {
            setY(getY() - getSpeed());
        }
        if (down) {
            setY(getY() + getSpeed());
        }
        if (left) {
            setX(getX() - getSpeed());
        }
        if (right) {
            setX(getX() + getSpeed());
        }
        if (up || down || left || right) {
            moveCount++;
            if (moveCount >= 18) {
                moveCount = 0;
            }
            setImg(imgs[moveCount/3]);
        } else {
            moveCount = 0;
            setImg(imgs[moveCount]);
        }
    }

    // 按下
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_D:
                right = true;
                System.out.println("right");
                break;
            case KeyEvent.VK_A:
                left = true;
                System.out.println("left");
                break;
            case KeyEvent.VK_W:
                up = true;
                System.out.println("up");
                break;
            case KeyEvent.VK_S:
                down = true;
                System.out.println("down");
                break;
        }
    }

    // 释放
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_D:
                right = false;
                break;
            case KeyEvent.VK_A:
                left = false;
                break;
            case KeyEvent.VK_W:
                up = false;
                break;
            case KeyEvent.VK_S:
                down = false;
                break;
        }
    }
}
